package com.mvcplus.core.interceptor;

import com.mvcplus.common.constant.GlobalConstant;
import com.mvcplus.common.model.SessionInfo;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2016/10/27.
 */
public class SecurityInterceptor implements HandlerInterceptor {

    private List<String> excludeUrls;// 不需要拦截的资源

    public List<String> getExcludeUrls() {
        return excludeUrls;
    }

    public void setExcludeUrls(List<String> excludeUrls) {
        this.excludeUrls = excludeUrls;
    }

    /**
     * 在调用controller具体方法前拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestUri.substring(contextPath.length());
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
        //判断是否包含在菜单权限里

        if ((url.indexOf("/main/") > -1) || excludeUrls.contains(url)) {// 如果要访问的资源是不需要验证的
            return true;
        }

        if ((sessionInfo == null) || (sessionInfo.getId() == null)) {// 如果没有登录或登录超时
           // request.setAttribute("msg", "您还没有登录或登录已超时，请重新登录，然后再刷新本功能！");
            request.getRequestDispatcher("/common/noSession").forward(request, response);
            return false;
        }

        //开发模式关闭
       /* if(!sessionInfo.getResourceAllList().contains(url)){
            return true;
        }*/

        /*if (!sessionInfo.getResourceList().contains(url)) {// 如果当前用户没有访问此资源的权限
            request.setAttribute("msg", "您没有访问此资源的权限！<br/>请联系超管赋予您<br/>[" + url + "]<br/>的资源访问权限！");
            request.getRequestDispatcher("/common/noSecurity").forward(request, response);
            return false;
        }*/

        return true;
    }

    /**
     * 完成页面的render后调用
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
                                Exception exception) throws Exception {

    }

    /**
     * 在调用controller具体方法后拦截
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
                           ModelAndView modelAndView) throws Exception {

    }


}
